import 'package:flutter/material.dart';

Color hexToColor(String hexString) {
  if (hexString.isEmpty) return Colors.transparent;
  if (hexString.contains("rgb")) {
    final t = hexString.substring(5, hexString.length - 1);
    final list = t.split(",").map((e) => double.parse(e)).toList();
    return Color.fromRGBO(
      list[0].toInt(),
      list[1].toInt(),
      list[2].toInt(),
      list[3],
    );
  } else {
    final buffer = StringBuffer();
    if (hexString.length == 4) {
      buffer.write(
        "${hexString.replaceFirst('#', '')}${hexString.replaceFirst('#', '')}",
      );
    }
    if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
    buffer.write(hexString.replaceFirst('#', ''));
    return Color(int.parse(buffer.toString(), radix: 16));
  }
}
